﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：吕海乐 
    //
    // 文件名：MessageAdd.cs
    // 文件功能描述：消息管理：新增消息
    // 创建标识：吕海乐2009-5-11
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
// 审核时间：
// 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Admin.MessageManage
{
    public partial class MessageAdd : BasePage
    {
        IBLL_Message im = Factory.BLL_Message();

        public MessageAdd()
        {
            this.ModuleNo = Module.消息管理.ToString();
            this.PageOperateType = OperateType.写消息.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            tdSender.InnerText = CurrentAccount.GetUser().UserID;
            txtRecivedPerson.Attributes.Add("readonly", "readonly");
        }


        /// <summary>
        /// 发送消息
        /// </summary>
        private void SendMessage()
        {
            IList<Message> lstMsg = new List<Message>();
            string reciverIds = hfdRecivedPerson.Value.TrimEnd(',');
            if (string.IsNullOrEmpty(reciverIds) || string.IsNullOrEmpty(txtRecivedPerson.Text.Trim()))
            {
                HZ.Web.WebUtil.Alert("请选择收件人！", this);
                txtRecivedPerson.Focus();
                return;
            }

            if (string.IsNullOrEmpty(txtTitle.Text.Trim()))
            {
                HZ.Web.WebUtil.Alert("标题不能为空！", this);
                return;
            }

            if (string.IsNullOrEmpty(txtContent.Value))
            {
                HZ.Web.WebUtil.Alert("消息内容不能为空！", this);
                return;
            }

            string[] arrayIds = reciverIds.Split(',');

            //剔除重复的
            List<string> list = new List<string>();

            foreach (var item in arrayIds)
            {
                if (!list.Contains(item))
                {
                    list.Add(item);
                }
            }

            foreach (string item in list)
            {
                Message aMsg = new Message();
                aMsg.Content = txtContent.Value;
                aMsg.MessageID = Guid.NewGuid().ToString();
                aMsg.Reciever = item;
                aMsg.Sender = tdSender.InnerText;
                aMsg.SendTime = DateTime.Now;
                aMsg.Status = 0;
                aMsg.Title = txtTitle.Text;
                lstMsg.Add(aMsg);
            }


            int flag = 0;

            try
            {
                if (cbxSaveToSendBox.Checked)
                {
                    flag = im.AddMessage_Save_Batch(lstMsg);
                }
                else
                {
                    flag = im.AddMessage_Batch(lstMsg);

                }
            }
            catch (Exception ex)
            {
                HZ.Web.WebUtil.Alert(ex.Message);
            }

            if (flag == 1)
            {
                PageInfo("消息发送成功！", "MessageGet.aspx");
            }
            else
            {
                PageError("消息发送失败！", this.ReturnUrl);
            }
        }

        /// <summary>
        /// 发送信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            SendMessage();
        }

    }
}
